package com.video.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.video.bean.UserInfo;
import com.video.bean.Vip;
import com.video.dao.UserInfoDao;
import com.video.dao.VipDao;
import com.video.service.VipServiceI;
import com.video.utils.DateConfig;
import com.video.utils.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VipServiceImpl implements VipServiceI {
    @Autowired
    private VipDao vipDao;

    @Override
    public ResultMap selectAllVip() {
        List<Vip> vip = vipDao.selectAllVip();
        if(vip != null){
            return ResultMap.createBySuccess(vip);
        }else{
            return ResultMap.createByError("暂无任何vip！");
        }
    }

    @Override
    public ResultMap createVip(String vipStr) {
        Vip vip = JSONObject.parseObject(vipStr,Vip.class);
        vip.setVtId(UUID.randomUUID().toString());
        vipDao.createVip(vip);
        return ResultMap.createBySuccess();
    }

    @Override
    public ResultMap updateVipByVtId(String vipStr) {
        Vip vip = JSONObject.parseObject(vipStr,Vip.class);
        vipDao.updateVipByVtId(vip);
        return ResultMap.createBySuccess();
    }

    @Override
    public ResultMap deleteVipByVtid(String vtId) {
        //这里要加业务逻辑
        vipDao.deleteVipByVtid(vtId);
        return ResultMap.createBySuccess();
    }

    @Override
    public ResultMap insertMember(String vtId, HttpSession session) {
        //生成user_vip关联表id
        String uvId = UUID.randomUUID().toString();
        //获取当前充值得会员持续时间
        Vip vip = vipDao.selectVipByVtid(vtId);
        int day = vip.getVipDay();
        //获取登陆用户得用户名
        UserInfo userInfo = (UserInfo) session.getAttribute("user");
        String username = userInfo.getUsername();

        //查询当前用户是否为vip
        Vip uservip = vipDao.selectVipByUsername(username);
        Date endTime = null;

        //查询user_vip表中是否存在此用户
        if(uservip == null){//不存在，让其充值
            uservip  = new Vip();
            uservip.setUvId(uvId);
            uservip.setUsername(username);
            uservip.setVtId(vtId);
            uservip.setStartTime(new Date());


            endTime = DateConfig.strToDate(DateConfig.plusToday(day));
            uservip.setEndTime(endTime);
            vipDao.insertMember(uservip);
            return ResultMap.createBySuccess("充值成功");
        }else{//已经存在
            //查询用户是否已经超出会员期限
            Date startTime = uservip.getStartTime();
            endTime = uservip.getEndTime();
            //将时间转换为字符串
            String startTimeStr = DateConfig.DateToStr(startTime);
            String endTimeStr = DateConfig.DateToStr(endTime);
            //判断两时间差
            long time = DateConfig.equalsDate(startTimeStr,endTimeStr);
            if(time >= 0){//还是会员，不用充值
                return ResultMap.createByError("您还是会员，不用充值");
            }else{//已经过期，可以充值
                uservip.setUvId(uvId);
                uservip.setUsername(username);
                uservip.setVtId(vtId);
                uservip.setStartTime(new Date());
                endTime = DateConfig.strToDate(DateConfig.plusToday(day));
                uservip.setEndTime(endTime);
                vipDao.insertMember(uservip);
                return ResultMap.createBySuccess("充值成功");
            }
        }
    }
}
